package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000117693050";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQCpwVhixaNfgN1QD9cZ5m1Lfpe0tmRxVQ9QRAE0EKRTJMMrG4AfHUcfFb5Gwsdr6tEBGzDjEm0Lh9dxQiwnEJTfWHknx4fHHxFY4OwRAwMUzuSkQy5THJ73BtvMKzRZg3KyGhwUTo73uwCjw+qp//nu7EtQKRGg99QPdkAXankuVw2747nMzJWJACjc9gnQxIN7i2A/yaHFHt1a62CapGF4p+uFek+rLXUCQuhQyZLEWS2xlIYnRh1J2Bt5X0V/+h4+gcVeeWjutUSey+20+j0nryjCMnFIkftSCLMYgLo2LkC7o3/80zMIxWTqxznDjJQ5qtKiDcRSyTLKoga/GBenAgMBAAECggEBAKUXxZbh8Z57HEG19hNZV6gZaP82d38FI4yDZsw09ytpUGpHJ5abCcVAhzoYSow45aV2xVfBXs3dAflDSt9/L9n7GHFIi1NrcpqGvO9ah4+gGxtpvs7TwgIq6G6rAhGgVlbCL9bMBB0IAHpEmp9UtMTZh/Fgz6VGATY8Yd6kDVCzgMoONhbDR3DoTcW4um75aIEL67VGeFXAcBMnvGQDOpUe1L91f98V0wT0dej1S2/GZPB/+gEQDKyhQRZKYhM4MmlWsbQRYpdXhDmD5qb5AslLbLRWstNBDB/SjdfatMB+pxWgoMO2wHtoURfxbWRyUfwZ+eVj5nJ3pYtipVJcMIECgYEA36jFBak2BFcAGnjhV8T6AGEWnxZLXPLhjtm+9ewQth9GVHB1v66bBqL6eh8xQUUoeY+Yn0cqs0j0/IxMpi3kdfmxr0UK/cxsE3XRHRRKs4Pceo4tAYBMOosw/xjiPFaLuliGUJroZqX6BnPrgKFuAMGYJRFAGx45I5l59ofJFmcCgYEAwk0z3rz5FlNdyWMkmq/wMGTFklY9i17KOv+AWwCkwt1IFfyHN9FlUkP9JUbC2waVU4c3YVi26P2XBW5dvdf6TGCFtFnBW09b04sSAW3A9U0W9xBF0QnFJ7I5H/LP8coDK/qAUqqGhonKnavGDopu6ZbY+xxfrHL9q3M0AW5nrMECgYEAuxsLSAMY52SI95xsl+p3Yt3V+NzDy0x9Ge5bbIxCJrnfMkCqoOLHWjP56zQxFnNuutNAUO9Ob5H3MVeFv/fgijmLRh8yZMQhHd6QRA13uNn1FqneSMU2ADv1j1MX4HZeilQIeWI+TbFIiyFqwL9LsWJDj67wvD76eieo+m758L0CgYEAoIq1nwa5MQ804N9AMWfJp9K7xEvpkiyCH/lYjtibSj8X/sKm3yHu++gdVB2BhwOLgnao9ZAdoalWjTBB2EiKEW13TCsVLmMF1maEPBX5ZzWJEh6p8SyHcGM7+//efUOTF1VljehEOwnDFkAF56HOPsdbBTDOhGiG3JDVmZElrIECgYEAzGuwociDsuHJZS9NdrON2PZ+hLlyNTV9At0ta1Gcm1eF49dY6izjb5ARlNa9r6pG9o8elEoRWjaCb+wpH7sJwXhA7J1a83B8i8f5R+KYgS1okmqAaecwc216Tg3V1z75Hk/nZvxsXH1WAEvzhdUypMJGplk+Rc1nAUp0VpO3cMw=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyKmry0/tKkck69miPQYlcwlRDx0kOyjNED3lLenT24xCw4gv45w6iN9uuXoTMTRkTV+5u++R+Pz3GzFsg0hIrBrXxqRG2/a9urgrchi8XAsN9Vdc5m3E04KKSA4EE+o/9t9MiJr3d5GuOwN+ZyRGYVTW8DbnBhfkAHfsVJsT0O8nbgqobrYWZDVmdbnMjn6dDHsvfW/phQxrDH3+R9yWkgcvT6MR395RozjI2cBED6xw3lOAp+pKwrIuJ702yWXi4yDfOdTqbUb5zxbokz2WjI0NYJqNYHvu7V4nYX2DXDhfDoVH7Hd8BKinpcfBuAL5i6iXf/UTw7NmqI42OIqITQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
